package com.crm.service.impl.callCenter;

import com.crm.dao.mapper.callCenter.CallFormMapper;
import com.crm.dao.mapper.customMapper.callCenterCustomMapper.CallFormCustomMapper;
import com.crm.model.entity.callCenter.CallForm;
import com.crm.model.qo.callCenter.CallFormQO;
import com.crm.model.qo.callCenter.CallMarkupQO;
import com.crm.model.vo.callCenter.*;
import com.crm.service.callCenter.CallFormDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 呼叫中心-呼叫表单
 *
 * @Author renshaorong
 * @Date 2021/4/1
 */
@Service
public class CallFormDbServiceImpl implements CallFormDbService {

    @Autowired
    private CallFormCustomMapper callFormCustomMapper;

    @Autowired
    private CallFormMapper callFormMapper;

    /**
     * 呼叫表单列表（全部）
     *
     * @param callFormQO 查询条件
     * @param dateUnit   时间类型（全部/本周/本月）
     * @param order      排序字段 类型
     * @return
     * @Author renshaorong
     * @Date 2021/4/1
     */
    @Override
    public List<CallFormVO> selectAllCallFormList(CallFormQO callFormQO, Integer dateUnit, String order) {
        return callFormCustomMapper.selectAllCallFormList(callFormQO, dateUnit, order);
    }

    /**
     * 呼叫表单列表（当前用户所在分公司）
     *
     * @param callFormQO 查询条件
     * @param dateUnit   时间类型（全部/本周/本月）
     * @param order      排序字段 类型
     * @return
     * @Author renshaorong
     * @Date 2021/4/1
     */
    @Override
    public List<CallFormVO> selectCallFormListByCompanyId(CallFormQO callFormQO, Integer dateUnit, String order) {
        return callFormCustomMapper.selectCallFormListByCompanyId(callFormQO, dateUnit, order);
    }

    /**
     * 呼叫表单列表（当前用户）
     *
     * @param callFormQO 查询条件
     * @param dateUnit   时间类型（全部/本周/本月）
     * @param order      排序字段 类型
     * @return
     * @Author renshaorong
     * @Date 2021/4/1
     */
    @Override
    public List<CallFormVO> selectCallFormListByUserId(CallFormQO callFormQO, Integer dateUnit, String order) {
        return callFormCustomMapper.selectCallFormListByUserId(callFormQO, dateUnit, order);
    }


    @Override
    public CallForm selectCallFormByFormId(String formId) {
        return callFormMapper.selectByPrimaryKey(formId);
    }


    /**
     * 呼叫详情列表（全部）
     *
     * @param callFormQO 查询条件
     * @param dateUnit   时间类型（全部/本周/本月）
     * @param order      排序字段 类型
     * @return
     */
    @Override
    public List<CallFormVO> selectAllCallDetailsList(CallFormQO callFormQO, Integer dateUnit, String order) {
        return callFormCustomMapper.selectAllCallDetailsList(callFormQO, dateUnit, order);
    }

    /**
     * 呼叫表单列表（当前用户所在分公司）
     *
     * @param callFormQO 查询条件
     * @param dateUnit   时间类型（全部/本周/本月）
     * @param order      排序字段 类型
     * @return
     */
    @Override
    public List<CallFormVO> selectCallDetailsListByCompanyId(CallFormQO callFormQO, Integer dateUnit, String order) {
        return callFormCustomMapper.selectCallDetailsListByCompanyId(callFormQO, dateUnit, order);
    }

    /**
     * 呼叫表单列表（当前用户）
     *
     * @param callFormQO 查询条件
     * @param dateUnit   时间类型（全部/本周/本月）
     * @param order      排序字段 类型
     * @return
     */
    @Override
    public List<CallFormVO> selectCallDetailsListByUserId(CallFormQO callFormQO, Integer dateUnit, String order) {
        return callFormCustomMapper.selectCallDetailsListByUserId(callFormQO, dateUnit, order);
    }

    @Override
    public Boolean insertCallForm(CallForm callForm) {
        Boolean result = false;
        int num = callFormMapper.insertSelective(callForm);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public Boolean updateLabel(String formId, Integer label) {
        CallForm callForm = new CallForm();
        callForm.setFormId(formId);
        callForm.setLabel(label);
        return callFormMapper.updateByPrimaryKeySelective(callForm) > 0;
    }

    /**
     * 折线图（接听、未接听、接听总数三条折线）
     *
     * @param dateUnit
     * @param type
     * @param accountId
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     */
    @Override
    public List<Integer> getPolyLineByAnswers(Integer dateUnit, Integer type, String accountId) {
        return callFormCustomMapper.getPolyLineByAnswers(dateUnit, type, accountId);
    }

    /**
     * 饼状图（30s/60s/90s/大于90s）
     *
     * @param dateUnit
     * @param accountId
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     */
    @Override
    public List<Map> getMinutePieData(Integer dateUnit, String accountId) {
        return callFormCustomMapper.getMinutePieData(dateUnit, accountId);
    }

    /**
     * 统计表（标签：1 意向电话 2 无意向电话 3 未标记电话 4 抽取标签电话 5 产生话术电话 6 恶意回复电话）
     *
     * @param dateUnit
     * @param accountId
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     */
    @Override
    public CallReportVO selectcallReportVOList(Integer dateUnit, String accountId, Integer line) {
        return callFormCustomMapper.selectcallReportVOList(dateUnit, accountId,line);
    }

    /**
     * 获取时间列表
     *
     * @param dateUnit
     * @return
     * @Author renshaorong
     * @Date 2021/4/16
     */
    public List<String> getTimeList(Integer dateUnit) {
        return callFormCustomMapper.getTimeList(dateUnit);
    }

    /**
     * @Description: 呼叫通话次数统计
     * @Param: [accountId, startTm, endTm, delStatus]
     * @Return: CallStatisticsVO
     * @Author: lixq-f
     * @Date: 2021/4/28
     **/
    @Override
    public CallStatisticsVO selectCallStatistics(String accountId, Date startTm,
                                                 Date endTm) {
        return callFormCustomMapper.selectCallStatistics(accountId, startTm, endTm);
    }


    @Override
    public List<CallVO<BigDecimal>> selectCallAverageTimesByDay(String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectCallAverageTimesByDay(companyId, startTm, endTm);
    }


    @Override
    public List<CallVO<BigDecimal>> selectCallAverageTimesByMonth(String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectCallAverageTimesByMonth(companyId, startTm, endTm);
    }


    @Override
    public List<CallVO<Long>> selectEmpCallTimesByDay(String accountId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectEmpCallTimesByDay(accountId, startTm, endTm);
    }

    @Override
    public List<CallVO<Long>> selectEmpCallTimesByMonth(String accountId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectEmpCallTimesByMonth(accountId, startTm, endTm);
    }


    @Override
    public CallDurationVO selectCallDurationStatistics(String accountId, String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectCallDurationStatistics(accountId, companyId, startTm, endTm);
    }


    @Override
    public Integer selectge90SecendsRank(String accountId, String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectge90SecendsRank(accountId,companyId,startTm,endTm);
    }

    @Override
    public Integer selectge60lt90SecendsRank(String accountId, String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectge60lt90SecendsRank(accountId,companyId,startTm,endTm);
    }

    @Override
    public Integer selectge30lt60SecendsRank(String accountId, String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectge30lt60SecendsRank(accountId,companyId,startTm,endTm);
    }

    @Override
    public Integer selectlt30SecendsRank(String accountId, String companyId, Date startTm, Date endTm) {
        return callFormCustomMapper.selectlt30SecendsRank(accountId,companyId,startTm,endTm);
    }


    @Override
    public boolean callMarkup(CallMarkupQO markupQO) {
        return callFormCustomMapper.callMarkup(markupQO) > 0;
    }


    @Override
    public boolean updateContact(CallFormQO callFormQO) {
        CallForm callForm = new CallForm();
        callForm.setFormId(callFormQO.getFormId());
        callForm.setContactName(callFormQO.getContactPersonName());
        return callFormMapper.updateByPrimaryKeySelective(callForm) > 0;
    }


}
